package com.huaru.message.config;

import com.alibaba.fastjson.JSONObject;
import com.glch.fkj398.msgservice.handler.Message.MsgCallBackImpl;
import com.glch.fkj398.msgservice.handler.netty.StartupEvent;
import com.huaru.message.scheduling.CourseInfoThred;
import com.huaru.message.utile.CourseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.huaru.message.config.StartMessage.COURSEINFOTHRED;

/**
 * Author: hth
 * Remark:接收服务端信息
 * Time: 2023/6/6 15:22
 */
@Component
@Order(2)
public class ReceiveMessage  implements CommandLineRunner {
    public static Integer PID = 0;
    @Override
    public void run(String... args) throws Exception {

        Thread receiveThread = new Thread(new ReceiveThread(), "ReceiveThread");
//        receiveThread.setPriority();
        receiveThread.start();
    }
    public class ReceiveThread implements Runnable{
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(100);
                    while (MsgCallBackImpl.vectorBuffer.size() > 0) {
                        //接收消息
                        byte[] bytes = (byte[]) MsgCallBackImpl.vectorBuffer.remove(0);
                        //......在这里写业务代码.......
                        String s = new String(bytes);
                        JSONObject json = JSONObject.parseObject(s.trim());
                        System.out.println(">>>>>"+json.toJSONString());
                        //TODO  接受消息 处理业务逻辑  根据消息头部
                        /**
                         * 1，启动软件
                         * 2，杀死进程
                         * 3,获取某个进程信息
                         * 4,关闭获取某个进程信息
                         */
                        String packType = json.getString("packType");
                        //判断消息头是杀掉进程,调用工具类杀掉某个进程
                        if(packType.equals("KillCourse")){
                            JSONObject data = json.getJSONObject("data");
                            Integer processId = data.getInteger("pid");
                            if(COURSEINFOTHRED.isAlive()){
                                COURSEINFOTHRED.stop();
                            }
                            CourseUtils.kilkCourseByPid(processId);
                        }
                        //判断类型是获取单个进程信息的消息头 开启线程获取
                        if(packType.equals("OpenCourse")){
                            JSONObject data = json.getJSONObject("data");
                            Integer courseId = data.getInteger("pid");
                            //每次打开进程信息前 先停止线程
                            COURSEINFOTHRED.stop();
                            PID = courseId;
                            COURSEINFOTHRED =  new CourseInfoThred();
                            COURSEINFOTHRED.start();
                        }
                        //判断类型是关闭获取单个进程信息的消息头 关闭线程获取
                        if(packType.equals("CloseCourse")){
                            if(COURSEINFOTHRED.isAlive()){
                                COURSEINFOTHRED.stop();
                            }

                        }
                        System.out.println(">>>>>>>>>"+json.toJSONString());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
